package com.momo.service;

import com.momo.res.JSONResult;
import com.momo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by Kagome on 2019/5/20.
 */
@Service
@Slf4j
public class WxMpTemplateMessageService {

    private WxMpService wxMpService;
    @Autowired
    private RedisTemplate<String, String> redisTemplateString;
    @Autowired
    private RedisUtil redisUtil;

    public WxMpTemplateMessageService() {

    }

    @Autowired
    public WxMpTemplateMessageService(WxMpService wxMpService, RedisTemplate<String, String> redisTemplateString) {
        this.wxMpService = wxMpService;
        this.redisTemplateString = redisTemplateString;
    }

    public JSONResult wxMpTemplateMessage(String openID)  {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openID)
                .templateId("1v2sJQW6UaVvf05DvDOeCZ9lEqXYsCtc9mtjhzJT3Kg")
                .url("https://mp.weixin.qq.com/mp/homepage?__biz=Mzg3MDIwNTA4Mg==&hid=2&sn=5f5eba67ad96eb0b4629974f62601e0e")
                .build();

        templateMessage.addData(new WxMpTemplateData("name1", "value1", "red"));
        templateMessage.addData(new WxMpTemplateData("name2", "value2", "red"));

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (WxErrorException e){
            e.getMessage();
            log.error("发送模板消息异常: {} {} {}", e.getMessage(),e.toString(),e);
            return JSONResult.errorMap("发送模板消息异常");
        }
        return JSONResult.ok("发送模板消息成功");

    }
}
